<template>
  <div class="default-main ba-table-box page">
    <!-- 按钮与搜索 -->
    <el-row class="page-button">
      <el-button type="primary" @click="save()">新增</el-button>
      <el-button type="success" @click="loadTableData()">刷新</el-button>
      <el-input style="width: 20%; margin: 0 0 0 auto" v-model="searchText" placeholder="搜索" :suffix-icon="Search"
        @keyup.enter="handleSearch" clearable />
    </el-row>

    <!-- 表格 -->
    <el-table :data="page.list" stripe border v-loading="tableLoading">
      <el-table-column fixed type="index" label="序号" width="100" align="center" />
      <el-table-column show-overflow-tooltip prop="name" label="银行名称" width="300" align="center">
        <template #default="scope">
          <span>{{ scope.row.code }}-{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip prop="type" label="协议类型" align="center" />
      <el-table-column show-overflow-tooltip prop="port" label="端口" align="center" />
      <el-table-column show-overflow-tooltip prop="charset" label="编码" align="center" />
      <el-table-column show-overflow-tooltip prop="url" label="url" align="center" />
      <el-table-column show-overflow-tooltip prop="failureRate" label="失败率" align="center">
        <template #default="scope">
          <span>{{ scope.row.failureRate }}%</span>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip prop="status" label="状态" align="center">
        <template #default="scope">
          <span v-if="scope.row.status === '1'">运行</span>
          <span v-else>停止</span>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip fixed="right" label="操作" align="center" width="150">
        <template #default="scope">

          <el-button width="50" link v-if="scope.row.status === '0'" type="success" size="small"
            @click="startOrStop(scope.row)">
            启动
          </el-button>
          <el-button width="50" link v-if="scope.row.status === '1'" type="primary" size="small"
            @click="startOrStop(scope.row)">
            停止
          </el-button>

          <el-button width="50" link type="warning" size="small" @click="edit(scope.row)">
            编辑
          </el-button>
          <el-popconfirm title="确认删除当前数据?" @confirm="del(scope.row)">
            <template #reference>
              <el-button width="50" link type="danger" size="small"> 删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination style="margin-top: 16px" v-model:current-page="page.current" v-model:page-size="page.limit"
      :page-sizes="[5, 10, 20, 50]" :small="small" :disabled="disabled" :background="background"
      layout="total, sizes, prev, pager, next, jumper" :total="page.total" @size-change="handleSizeChange"
      @current-change="onTableCurrentChange" />

    <!-- 编辑弹窗 -->
    <el-dialog v-if="dialogVisible" v-model="dialogVisible" title="编辑" width="800px" @close="emptyData()">
      <el-form ref="formRef" :model="formData" label-width="120px" class="demo-dynamic">
        <el-form-item label="银行名称">
          <el-select v-model="formData.name" clearable filterable placeholder="银行类型">
            <el-option v-for="item in bankCodeList" :key="item" :label="item" :value="item" />
          </el-select>
        </el-form-item>
        <el-form-item label="协议类型">
          <el-select v-model="formData.type" clearable filterable placeholder="协议类型">
            <el-option v-for="item in agreement" :key="item" :label="item" :value="item" />
          </el-select>
        </el-form-item>
        <el-form-item label="端口">
          <el-input v-model="formData.port" clearable></el-input>
        </el-form-item>
        <el-form-item label="编码">
          <el-select v-model="formData.charset" clearable filterable placeholder="编码">
            <el-option v-for="item in encoding" :key="item" :label="item" :value="item" />
          </el-select>
        </el-form-item>
        <el-form-item v-if="formData.type === 'http'" label="url">
          <el-input v-model="formData.url" clearable></el-input>
        </el-form-item>
        <el-form-item label="失败率">
          <el-input-number size="small" v-model="formData.failureRate" :precision="0" :step="1" :max="50" :min="0" />
          %
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="submit()">确认</el-button>
          <el-button @click="emptyData()">取消</el-button>
        </span>
      </template>
    </el-dialog>


  </div>
</template>
<script setup>
import { Search } from "@element-plus/icons-vue";
import { ElMessage } from "element-plus";
import { onMounted, reactive, ref } from "vue";

const small = ref(false)
const background = ref(false)
const disabled = ref(false)
const dialogVisible = ref(false);
const tableLoading = ref(false);
const searchText = ref("");
const page = reactive({
  current: 1,
  limit: 10,
  total: 0,
  list: [],
});

const formData = reactive({
  id: "",
  code: "",
  name: "",
  type: "",
  port: "",
  charset: "",
  url: "",
  failureRate: "",
});

const agreement = ref(["http", "socket"])
const encoding = ref(["UTF-8", "GBK"])

// 获取银行类型
let bankCodeList = ref(["银行类型"])

// 删除
const del = (row) => {

  request({
    url: "/bank/delete/" + row.id,
    method: "delete"
  }).then((res) => {
    if (res.status === 200) {
      ElMessage({
        message: '删除成功.',
        type: 'success',
      })
    }
    loadTableData();
  })
};

// 启用或关闭端口
const startOrStop = (row) => {
  let url;
  if (row.status === '1') {
    url = "/bank/stop?id=" + row.id
  } else {
    url = "/bank/start?id=" + row.id
  }

  request({
    url: url,
    method: "get",
    params: {
      id: row.id
    }
  }).then((res) => {
    if (res.status === 200) {
      ElMessage({
        message: '状态修改成功',
        type: 'success',
      })
      loadTableData();
    }
  })
}


// 编辑
const edit = (row) => {
  dialogVisible.value = true;
  formData.type = row.type;
  formData.name = row.code + "-" + row.name;
  formData.port = row.port;
  formData.charset = row.charset;
  formData.url = row.url;
  formData.failureRate = row.failureRate;
  formData.id = row.id;
};

// 新增
const save = () => {
  dialogVisible.value = true;
}

// 清楚form表单数据
const emptyData = () => {
  dialogVisible.value = false;
  formData.type = "";
  formData.code = "";
  formData.name = "";
  formData.name = "";
  formData.port = "";
  formData.charset = "";
  formData.url = "";
  formData.failureRate = 0;
  formData.id = "";
}

// 保存
const submit = () => {
  request({
    url: "/bank/saveUpdate",
    method: "post",
    data: formData,
    headers: {}
  }).then((res) => {
    if (res.status === 200) {
      let data = res.data;
      if (data.code === 10000) {
        ElMessage({
          message: '保存成功.',
          type: 'success',
        })
        emptyData();
        loadTableData();
      } else {
        ElMessage({
          message: data.msg,
          type: 'error',
        })
      }
    }
  })


};

// 搜索
const handleSearch = (e) => {
  e.target.blur();
  loadTableData();
};

// 修改分页
const onTableCurrentChange = (val) => {
  page.current = val;
  loadTableData();
};
const handleSizeChange = (val) => {
  page.limit = val;
  page.current = 1;
  loadTableData();
}
// 查询表格数据
const loadTableData = () => {
  tableLoading.value = true;

  request({
    url: "/bank/list",
    method: "post",
    data: {
      "pageNumber": page.current,
      "pageSize": page.limit,
      "query": searchText.value,
      "order": [
        {
          "field": "status",
          "sort": "DESC"
        },
        {
          "field": "code"
        }
      ]
    }
  }).then((res) => {
    if (res.status === 200) {
      let data = res.data;
      page.current = data.page;
      page.total = data.total;
      page.list = data.list;
    }
  }).finally(() => {
    tableLoading.value = false;
  });

};

const findBankCode = () => {
  request({
    url: "/bank/getBankCode",
    method: "get"
  }).then((res) => {
    if (res.status === 200) {
      let data = res.data;
      bankCodeList.value = data.data
    }
  })
}

// 页面挂载
onMounted(() => {
  loadTableData();

  findBankCode();

});
</script>
<style scoped>
.page {
  width: calc(100% - var(--ba-main-space) - var(--ba-main-space));
  padding: 16px;
  background: #ffffff;
}

.page-button {
  padding: 16px 16px 16px 0;
}
</style>
